package com.onstar.cn.upload.crash;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 6335yz on 2016/10/10.
 */
public class FileUpload {

    private static Context mContext;
    private RequestQueue mQueue;

    private FileUpload() {
        mQueue = Volley.newRequestQueue(mContext);
    }

    public static FileUpload getInstance(Context context) {
        mContext = context.getApplicationContext();
        return Inner.instance;
    }

    static class Inner {
        static final FileUpload instance = new FileUpload();
    }

    static class ErrorStringRequest extends StringRequest {

        private String mError;

        public ErrorStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener, String error) {
            super(method, url, listener, errorListener);
            mError = error;
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put("json", dumpPhoneInfo(mError));
            return map;
        }

        private String dumpPhoneInfo(String error) {
            try {
                StringBuilder sb = new StringBuilder();
                PackageManager pm = mContext.getPackageManager();
                PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
                sb.append("App Version:");
                sb.append(pi.versionName);
                sb.append("_");
                sb.append(pi.versionCode);

                sb.append(" ");
                sb.append("OS Version:");
                sb.append(Build.VERSION.RELEASE);
                sb.append("_");
                sb.append(Build.VERSION.SDK_INT);

                sb.append(" ");
                sb.append("Vendor:");
                sb.append(Build.MANUFACTURER);

                sb.append(" ");
                sb.append("Model:");
                sb.append(Build.MODEL);

                sb.append(" ");
                sb.append("CPU ABI:");
                sb.append(Build.CPU_ABI);

                sb.append(" ");
                sb.append("error stack:");
                sb.append(error);
                return sb.toString();
            } catch (Exception e) {
            }
            return error;
        }
    }

    /**
     * 获取本机的IP
     *
     * @return Ip地址
     */
    public static String getLocalHostIP() {
        String ip;
        try {
            /**返回本地主机。*/
            InetAddress addr = InetAddress.getLocalHost();
            /**返回 IP 地址字符串（以文本表现形式）*/
            ip = addr.getHostAddress();
        } catch (Exception ex) {
            ip = "";
        }
//        ip = "10.16.142.181";
        ip = "HQITL4026.HQ.ShanghaiOnStar.com";
        return ip;
    }

    public void upload(final String error) {// 10.16.142.181
        String url = "http://" + getLocalHostIP() + ":8080/FileUploadDemo/FileUploadServlet";
        ErrorStringRequest stringRequest = new ErrorStringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("TAG", "response -> " + response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("TAG", "VolleyError -> " + error);
            }
        }, error);
        mQueue.add(stringRequest);
        SystemClock.sleep(1000);
    }
//    private String dumpPhoneInfo(String error) {
//        try {
//            StringBuilder sb = new StringBuilder();
//            PackageManager pm = mContext.getPackageManager();
//            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
//            sb.append("App Version:");
//            sb.append(pi.versionName);
//            sb.append("_");
//            sb.append(pi.versionCode);
//
//            sb.append(" ");
//            sb.append("OS Version:");
//            sb.append(Build.VERSION.RELEASE);
//            sb.append("_");
//            sb.append(Build.VERSION.SDK_INT);
//
//            sb.append(" ");
//            sb.append("Vendor:");
//            sb.append(Build.MANUFACTURER);
//
//            sb.append(" ");
//            sb.append("Model:");
//            sb.append(Build.MODEL);
//
//            sb.append(" ");
//            sb.append("CPU ABI:");
//            sb.append(Build.CPU_ABI);
//
//            sb.append(" ");
//            sb.append("error stack:");
//            sb.append(error);
//            return sb.toString();
//        } catch (Exception e) {
//        }
//        return error;
//    }
}
